﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using Table_Dal;
namespace ApplicationLayer
{
    public partial class frmDeThi : DevComponents.DotNetBar.OfficeForm
    {
        string User = "";
        public frmDeThi(string id)
        {
            User = id;
            InitializeComponent();
        }

        private void cboMonHoc_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmTaoDeThi frm = new frmTaoDeThi(User);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("Tạo Đề Thi thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                loadlvw();
                loaddgv();
            }
        }
        ChitietdethiBLL ctdtbll;
        DethiBLL dtbll;
        MonHocBLL mhbll;
        List<MonHocTbl> dsmh;
        private void frmDeThi_Load(object sender, EventArgs e)
        {
            LoadCbo();
        }
        public void LoadCbo()
        {
            cboMonHoc.DataSource = null;
            mhbll = new MonHocBLL();
            dsmh = mhbll.LayTatCaMonHoc();
            cboMonHoc.DataSource = dsmh;
            cboMonHoc.DisplayMember = "TENMONHOC";
            cboMonHoc.ValueMember= "MaMonHoc";
            if(cboMonHoc.Items.Count>0)
            cboMonHoc.SelectedIndex = 0;
        }

        private void cboMonHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            dgvThiSinh.DataSource = null;
            loadlvw();
        }

        private void loadlvw()
        {
            MonHocTbl dt = dsmh[cboMonHoc.SelectedIndex];
            dtbll = new DethiBLL();
            List<DeThiTbl> ds = dtbll.LayDanhSachDeThi_MaMonHoc(dt.MaMonHoc,"");
            lvwDeThi.Items.Clear();
            foreach (DeThiTbl item in ds)
            {
                ListViewItem temp = new ListViewItem();
                temp.Text = item.MADETHI;
                temp.SubItems.Add(item.TENDETHI);
                temp.SubItems.Add(item.SOCAUHOI.ToString());
                temp.SubItems.Add(item.THOIGIAN+" phút");
                temp.SubItems.Add(item.NGAYTAODE.ToString("dd-MM-yyyy"));
                lvwDeThi.Items.Add(temp);
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (lvwDeThi.SelectedItems.Count == 0)
            {
                MessageBox.Show("Bạn chưa chọn Đề Thi", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else if (lvwDeThi.SelectedItems.Count > 1)
            {
                MessageBox.Show("Bạn chỉ được chọn 1 Đề Thi để Export", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                Obj_frmCaNhan.frmExportCauHoi frm = new Obj_frmCaNhan.frmExportCauHoi(lvwDeThi.SelectedItems[0].Text);
                frm.ShowDialog();

            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (lvwDeThi.SelectedItems.Count == 0)
            {
                MessageBox.Show("Bạn chưa chọn Đề Thi", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else if(MessageBox.Show("Bạn muốn xóa đề đã chọn chứ?","Thông báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==System.Windows.Forms.DialogResult.Yes)
            {
                for (int i = 0; i < lvwDeThi.SelectedItems.Count; i++)
                {
                    ctdtbll = new ChitietdethiBLL();
                    ctdtbll.Xoa(lvwDeThi.SelectedItems[i].Text);
                    dtbll = new DethiBLL();
                    dtbll.XoaDethi(lvwDeThi.SelectedItems[i].Text);
                }
            }
            loadlvw();
            loaddgv();
            MessageBox.Show("Xóa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        string tempt = "";
        private void lvwDeThi_SelectedIndexChanged(object sender, EventArgs e)
        {
            loaddgv();
        }

        private void loaddgv()
        {
            ThiSinhBLL tsbll = new ThiSinhBLL();
            if (lvwDeThi.SelectedItems.Count == 0)
            {
                dgvThiSinh.DataSource = null;
                return;
            }
            string t = "";
            for (int i = 0; i < lvwDeThi.SelectedItems.Count; i++)
            {
                t = lvwDeThi.SelectedItems[i].Text;
            }
            tempt = t;
            List<ThiSinhTbl> dsts = tsbll.LayTatCaThiSinh_Diem(t);

            dgvThiSinh.DataSource = dsts;
            dgvThiSinh.Columns["MATKHAU"].HeaderText = "Điểm";
            dgvThiSinh.Columns["TENSV"].Width = 200;
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (lvwDeThi.SelectedItems.Count == 0 || dgvThiSinh.Rows.Count == 0)
                return;
            if (MessageBox.Show("Bạn thật sự muốn xóa?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                lstMssv.Clear();
                for (int i = 0; i < dgvThiSinh.SelectedCells.Count; i++)
                {
                    if (KiemTra(dgvThiSinh.Rows[dgvThiSinh.SelectedCells[i].RowIndex].Cells[0].Value.ToString()) == false)
                        lstMssv.Add(dgvThiSinh.Rows[dgvThiSinh.SelectedCells[i].RowIndex].Cells[0].Value.ToString());
                }
                DiemBLL d = new DiemBLL();
                foreach (string item in lstMssv)
                {
                    d.Xoa(item, tempt);
                }
                loaddgv();
            }
        }
        List<string> lstMa = new List<string>();
        private void buttonX3_Click(object sender, EventArgs e)
        {
            if (lvwDeThi.SelectedItems.Count == 0)
            {
                MessageBox.Show("Bạn chưa chọn Đề thi", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            lstMa.Clear();
            for (int i = 0; i < dgvThiSinh.Rows.Count; i++)
            {
                lstMa.Add(dgvThiSinh.Rows[i].Cells[0].Value.ToString());
            }
            ThiSinh.frmThemTS_DeThi frm = new ThiSinh.frmThemTS_DeThi(lstMa,tempt);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                loaddgv();
                MessageBox.Show("Thêm thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
        List<string> lstMssv = new List<string>();
        public bool KiemTra(string k)
        {
            foreach (string item in lstMssv)
            {
                if (item.Equals(k) == true)
                    return true;
            }
            return false;
        }
        private void dgvThiSinh_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            lstMssv.Clear();
            for (int i = 0; i < dgvThiSinh.SelectedCells.Count; i++)
            {
                if (KiemTra(dgvThiSinh.Rows[e.RowIndex].Cells[0].Value.ToString()) == false)
                    lstMssv.Add(dgvThiSinh.Rows[e.RowIndex].Cells[0].Value.ToString());
            }
        }

        private void btnThemGT_Click(object sender, EventArgs e)
        {
            frmRaDeGiaiThuat frm = new frmRaDeGiaiThuat(User);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                frmGiaiThuatGEN frm1 = new frmGiaiThuatGEN(frm.DeThi_GT(), frm.MonHoc_Gt(), frm.ArrlstChuong(), frm.GiaiThuat(),User);
                if (frm1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    MessageBox.Show("Tạo Đề Thi thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadlvw();
                    loaddgv();
                }
            }
        }

        
    }
}